<!DOCTYPE html>
<html>
<head>
    {include file="_Fragment/meta" /}
    <link rel="stylesheet" href="/static/pear/component/pear/css/pear.css" />
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" lay-filter="reload">
            <div class="layui-form-item">
                <div class="layui-form-item layui-inline">
                    <div class="layui-input-inline">
                        <ul id="tree" class="dtree" data-id="0"></ul>
                    </div>
                </div>

                <div class="layui-form-item layui-inline">
                    <div class="layui-input-inline">
                        <input type="text" name="key" placeholder="工号|姓名" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item layui-inline">
                    <div class="layui-input-inline">
                        <input type="text" name="time" id="date" placeholder="时间" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item layui-inline">
                    <div class="layui-input-inline">
                        <select name="source">
                            <option value="">数据来源</option>
                            <option value="1">小程序</option>
                            <option value="2">PC端</option>
                        </select>
                    </div>
                </div>

                <div class="layui-form-item layui-inline">
                    <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="user-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class=" layui-tab layui-tab-card">
        <ul class="layui-tab-title">
            <li>
                <a href="/School/record">轻学堂</a>
            </li>

            <li>
                <a href="/Classes/record">线下班级</a>
            </li>
            <li class="layui-this">
                E-learning
            </li>
            <li>
                <a href="/Live/record">直播课</a>
            </li>
        </ul>
        <div class="layui-card-body">
            <table id="user-table" lay-filter="user-table"></table>
        </div>
    </div>
</div>
{include file="_Fragment/javascript" /}
<script>
    layui.use(['table', 'form', 'jquery', 'dtree', 'laydate'], function() {
        let table = layui.table;
        let form = layui.form;
        let dtree = layui.dtree;
        let $ = layui.jquery;
        let laydate = layui.laydate;

        laydate.render({
            elem: '#date'
            ,range: true //或 range: '~' 来自定义分割字符
            ,trigger: 'click'
            ,type: 'date'
        });

        dtree.render({
            elem: "#tree",
            initLevel: "1",
            method: 'get',
            url: "/Members/getDepartment",
            select: true,
            selectTips: '部门',
        });


        let cols = [
            [
                {
                    title: '工号',
                    field: 'userid',
                    align: 'center',
                },
                {
                    title: '姓名',
                    field: 'name',
                    align: 'center',
                },

                {
                    title: 'Division',
                    field: 'division',
                    align: 'center',
                },
                {
                    title: '部门',
                    field: 'department_name',
                    align: 'center',
                },
                {
                    title: '已学课程',
                    field: 'learning_total',
                    align: 'center',
                    sort: true
                },
                {
                    title: '已学完课程',
                    field: 'finished_total',
                    align: 'center',
                },
                {
                    title: '总学习时长(s)',
                    field: 'duration',
                    align: 'center',
                    sort: true
                },
            ]
        ]

        table.render({
            elem: '#user-table',
            url: '/Learning/getRecordList',
            page: {layout:['count', 'prev', 'page', 'next', 'skip']},
            limit: 5,
            cols: cols,
            //skin: 'line',
            toolbar: '#user-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter',{
                title: '导出课程学习明细' //标题
                ,layEvent: 'export' //事件名，用于 toolbar 事件中使用
                ,icon: 'layui-icon-export' //图标类名
            }]
        });

        form.on('submit(user-query)', function(data) {
            table.reload('user-table', {
                where: data.field
                ,page: {
                    curr: 1, //重新从第 1 页开始
                    layout:['count', 'prev', 'page', 'next', 'skip']
                }
                ,limit: 10,
            })
            return false;
        });

        //监听排序事件
        table.on('sort(user-table)', function(obj){ //注：sort 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var field = form.val('reload');
            table.reload('user-table', {
                initSort: obj //记录初始排序，如果不设的话，将无法标记表头的排序状态。
                ,where: { //请求参数（注意：这里面的参数可任意定义，并非下面固定的格式）
                    field: obj.field //排序字段
                    ,order: obj.type //排序方式
                    ,tree_select_nodeId: field.tree_select_nodeId
                    ,key: field.key
                    ,time: field.time
                    ,source: field.source
                }
            });
        });



        table.on('toolbar(user-table)', function(obj) {
            if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'export') {
                window.export();
            }
        });


        // 数据导出
        // window.export = function() {
        //     var field = form.val('reload');
        //
        //     layer.confirm('确定要导出课程学习明细吗?', {
        //         icon: 3,
        //         title: '提示'
        //     }, function(index) {
        //         layer.close(index);
        //         window.open("{:url('exportRecord')}?tree_select_nodeId="+field.tree_select_nodeId+"&key="+field.key+"&time="+field.time, "_blank");
        //     });
        // };

        // 数据导出
        window.export = function() {
            var field = form.val('reload');
            layer.confirm('确定要导出课程学习明细吗?', {
                icon: 3,
                title: '提示'
            }, function(index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: "/Learning/exportRecord",
                    data:{key:field.key, source:field.source, tree_select_nodeId:field.tree_select_nodeId, time:field.time},
                    dataType: 'json',
                    type: 'post',
                    success: function(result) {
                        layer.close(loading);
                        if (result.status == 0) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 2000
                            });
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 2000
                            });
                        }
                    }
                })
            });
        };


        window.refresh = function(param) {
            table.reload('user-table');
        }
    })
</script>
</body>
</html>
